package sketchpad.gui.component;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import sketchpad.business.function.ComposeService;
import sketchpad.business.function.OperateService;
import sketchpad.business.function.impl.ComposeServiceImpl;
import sketchpad.business.function.impl.OperateServiceImpl;
import sketchpad.gui.control.DrawControl;
import sketchpad.gui.component.event.EventCenter;

public class PopupMenu {

    private DrawControl control;
    private ContextMenu popup;
    private Stage stage;

    private ComposeService composeService;
    private OperateService operateService;

    public PopupMenu(DrawControl drawControl, Stage primary){
        stage = primary;
        control = drawControl;

        composeService = new ComposeServiceImpl(control);
        operateService = new OperateServiceImpl(control);

        stage.setAlwaysOnTop(true);
        stage.setResizable(false);
        stage.setTitle("请选择标签");
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.getIcons().add(new Image(getClass().getResource("/")+"/icon/画板.png"));

        popup = new ContextMenu();
        addJFXButton(popup);
        popup.setAutoFix(true);
        popup.setAutoHide(true);
    }

    public void show(double x, double y){
        popup.setAnchorX(x+control.getStage().getX()+10);
        popup.setAnchorY(y+control.getStage().getY()+100);
        popup.show(control.getStage());
    }

    private void addJFXButton(ContextMenu popup){
        Menu operateMenu = new Menu("鼠标");
        ImageView view = new ImageView(new Image(getClass().getResource("/")+"/icon/鼠标.png"));
        view.setFitHeight(20);
        view.setFitWidth(20);
        operateMenu.setGraphic(view);
        MenuItem copy = new MenuItem("复制");
        copy.setOnAction(event -> operateService.copy());
        MenuItem paste = new MenuItem("粘贴");
        paste.setOnAction(event -> operateService.paste());
        operateMenu.getItems().add(copy);
        operateMenu.getItems().add(paste);
        popup.getItems().add(operateMenu);

        Menu memberMenu = new Menu("组合");
        ImageView view1 = new ImageView(new Image(getClass().getResource("/")+"/icon/组合.png"));
        view1.setFitHeight(20);
        view1.setFitWidth(20);
        memberMenu.setGraphic(view1);
        MenuItem compose = new MenuItem("组合");
        compose.setOnAction(event -> composeService.compose());
        MenuItem decompose = new MenuItem("分解");
        decompose.setOnAction(event -> composeService.decompose());
        memberMenu.getItems().add(compose);
        memberMenu.getItems().add(decompose);
        popup.getItems().add(memberMenu);

        MenuItem label = new MenuItem("标记");
        label.setOnAction(event -> {
            EventCenter.labelEventHandler(stage,control);
        });
        ImageView view2 = new ImageView(new Image(getClass().getResource("/")+"/icon/标记.png"));
        view2.setFitHeight(20);
        view2.setFitWidth(20);
        label.setGraphic(view2);
        popup.getItems().add(label);

        MenuItem diff = new MenuItem("识别");
        diff.setOnAction(event -> {
            hidePopup();
            EventCenter.recognizeEventHandler(stage,control);
        });
        ImageView view3 = new ImageView(new Image(getClass().getResource("/")+"/icon/识别.png"));
        view3.setFitHeight(20);
        view3.setFitWidth(20);
        diff.setGraphic(view3);
        popup.getItems().add(diff);
    }

    public void hidePopup(){popup.hide();}
}
